package com.jpa.model;

import java.text.NumberFormat;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;

import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.data.Item;
import com.vaadin.ui.Table;

public class Report {
	private JPAContainer<?> income;
	private JPAContainer<?> outcome;
	private int total;
	private int price;
	private int balance;
	public ResourceBundle Messages;

	public Report(JPAContainer<?> income, JPAContainer<?> outcome,
			ResourceBundle Messages) {
		this.income = income;
		this.outcome = outcome;
		this.Messages = Messages;
	}

	public Report() {
		// TODO Auto-generated constructor stub
	}

	public void showTotal(Table table) {
		int n;
		int m;
		int t = 0;
		int y = 0;
		int a = 0;
		int ba = 0;
		income.refresh();
		outcome.refresh();
		String id = "month"; //$NON-NLS-1$
		for (int b = 1; b <= 12; b++) {
			int in = 0;
			int out = 0;
			Iterator<Object> o = outcome.getItemIds().iterator();
			while (o.hasNext()) {
				m = (Integer) o.next();
				Item item2 = outcome.getItem(m);
				if (((Integer) item2.getItemProperty(id).getValue()) == b) {
					price = (Integer) item2.getItemProperty("total").getValue(); //$NON-NLS-1$
					out = out + price;
					balance = in - out;
					table.removeItem(b);

					table.addItem(new Object[] { b, in, out, balance }, b);
				}
			}

			Iterator<Object> it = income.getItemIds().iterator();
			while (it.hasNext()) {
				n = (Integer) it.next();
				Item item = income.getItem(n);
				if (((Integer) item.getItemProperty(id).getValue()) == b) {

					total = (Integer) item.getItemProperty("total").getValue(); //$NON-NLS-1$
					in = in + total;
					balance = in - out;

					table.removeItem(b);

					table.addItem(new Object[] { b, in, out, balance }, b);

				}
			}

		}

		Iterator<?> itr = table.getItemIds().iterator();
		while (itr.hasNext()) {
			a = (Integer) itr.next();
			t += (Integer) table.getContainerProperty(a,
					Messages.getString("Report.in")).getValue(); //$NON-NLS-1$
			y += (Integer) table.getContainerProperty(a,
					Messages.getString("Report.out")).getValue(); //$NON-NLS-1$
			ba += (Integer) table.getContainerProperty(a,
					Messages.getString("Report.balance")).getValue(); //$NON-NLS-1$

		}
		table.setFooterVisible(true);
		table.setColumnFooter(
				Messages.getString("Report.month"), Messages.getString("Report.total")); //$NON-NLS-1$ //$NON-NLS-2$
		table.setColumnFooter(
				Messages.getString("Report.in"), NumberFormat.getNumberInstance(Locale.US).format(t)); //$NON-NLS-1$
		table.setColumnFooter(
				Messages.getString("Report.out"), NumberFormat.getNumberInstance(Locale.US).format(y)); //$NON-NLS-1$
		table.setColumnFooter(
				Messages.getString("Report.balance"), NumberFormat.getNumberInstance(Locale.US).format(ba)); //$NON-NLS-1$
		table.setPageLength(table.size());
	}

	public void showTotalwo(Table table) {
		int n;
		int m;
		int t = 0;
		int y = 0;
		int a = 0;
		int ba = 0;
		income.refresh();
		outcome.refresh();
		String id = "month"; //$NON-NLS-1$
		for (int b = 1; b <= 12; b++) {
			int in = 0;
			int out = 0;
			Iterator<Object> o = outcome.getItemIds().iterator();
			while (o.hasNext()) {
				m = (Integer) o.next();
				Item item2 = outcome.getItem(m);
				if (((Integer) item2.getItemProperty(id).getValue()) == b) {
					price = (Integer) item2.getItemProperty("price").getValue(); //$NON-NLS-1$
					out = out + price;
					balance = in - out;
					table.removeItem(b);

					table.addItem(new Object[] { b, in, out, balance }, b);
				}
			}

			Iterator<Object> it = income.getItemIds().iterator();
			while (it.hasNext()) {
				n = (Integer) it.next();
				Item item = income.getItem(n);
				if (((Integer) item.getItemProperty(id).getValue()) == b) {

					total = (Integer) item.getItemProperty("price").getValue(); //$NON-NLS-1$
					in = in + total;
					balance = in - out;

					table.removeItem(b);

					table.addItem(new Object[] { b, in, out, balance }, b);

				}
			}

		}
		Iterator<?> itr = table.getItemIds().iterator();
		while (itr.hasNext()) {
			a = (Integer) itr.next();
			t += (Integer) table.getContainerProperty(a,
					Messages.getString("Report.in")).getValue(); //$NON-NLS-1$
			y += (Integer) table.getContainerProperty(a,
					Messages.getString("Report.out")).getValue(); //$NON-NLS-1$
			ba += (Integer) table.getContainerProperty(a,
					Messages.getString("Report.balance")).getValue(); //$NON-NLS-1$

		}
		table.setFooterVisible(true);
		table.setColumnFooter(
				Messages.getString("Report.month"), Messages.getString("Report.total")); //$NON-NLS-1$ //$NON-NLS-2$
		table.setColumnFooter(
				Messages.getString("Report.in"), NumberFormat.getNumberInstance(Locale.US).format(t)); //$NON-NLS-1$
		table.setColumnFooter(
				Messages.getString("Report.out"), NumberFormat.getNumberInstance(Locale.US).format(y)); //$NON-NLS-1$
		table.setColumnFooter(
				Messages.getString("Report.balance"), NumberFormat.getNumberInstance(Locale.US).format(ba)); //$NON-NLS-1$
		table.setPageLength(table.size());
	}

}
